﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WMS.Infrastructure.BaseRepository;

namespace Zocono.WCS.Infrastructure.Repository
{
    public class RoleFunctionRepository : BaseRepository<RoleFunctionInfo>, IRoleFunctionRepository
    {
        public RoleFunctionRepository(SqlSugarClient context = null) : base(context)
        {

        }

        public async Task<List<RoleFunctionInfoAllShow>> GetRoleFunctionInfoAllTreeAsync(long roleID)
        {
            return await Task.Run(() => this.Context.Queryable<RoleFunctionInfo, RoleInfo, FunctionInfo>
            ((rf, r, f) => new JoinQueryInfos
               (JoinType.Left, rf.RoleID == r.ID,
               JoinType.Left, rf.FunctionID == f.ID))
            .Select((rf, r, f) => new RoleFunctionInfoAllShow { ID = rf.ID.SelectAll(), FullName = r.FullName, FullNameUrl = f.FullName, Url = f.URL, Symbol = f.Symbol })
            .ToTree(it => it.Child, it => it.ParentID, 0)
            .Where(it => it.IsPopUpWindow == false && it.RoleID == roleID)
            .OrderBy(it => it.TreeIndex)
            .ToList());
        }

        public async Task<List<RoleFunctionInfo>> GetRoleFunctionInfoTreeAsync(long roleID)
        {
            return await Task.Run(() => this.Context.Queryable<RoleFunctionInfo>()
             .Where(it => it.IsPopUpWindow == false && it.RoleID == roleID)
             .ToTree(it => it.Child, it => it.ParentID, 0)//以ParentID 的作为节点，0表示根节点
             .OrderBy(it => it.TreeIndex)//以TreeIndex排序
            .ToList());
        }

        public async Task<List<RoleFunctionInfo>> GetAllRoleFunctionInfoTreeAsync(long roleID)
        {

            return await this.Context.Queryable<RoleFunctionInfo>()
            .Where(it => it.RoleID == roleID).ToListAsync();
        }
        /// <summary>
        /// 获取角色菜单信息
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public async Task<List<RoleFunctionPage>> GetRoleFunctionByRoleId(long roleID)
        {
            return await this.Context.Queryable<RoleFunctionInfo, FunctionInfo>((rf, f) => new JoinQueryInfos
                (JoinType.Inner, rf.FunctionID == f.ID))
                   .Where((rf, f) => f.Status == 1 && rf.RoleID == roleID)
                   .OrderBy((rf, f) => new { f.Pid, rf.FunctionID })
                  .Select((rf, f) => new RoleFunctionPage { ID = rf.ID, RoleID = rf.RoleID, FunctionID = rf.FunctionID, FullName = f.FullName, Url = f.URL, PId = f.Pid, MenuIcon = f.MenuIcon })
                  .ToListAsync();
        }
        /// <summary>
        /// 修改角色权限信息
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public async Task<bool> UpdateRoleFunctionInfoByRoleIdAsync(List<RoleFunctionInfo> list)
        {
            Context.Deleteable<RoleFunctionInfo>().Where(it => it.RoleID == list[0].RoleID).ExecuteCommand();
            var row = await Context.Insertable(list).ExecuteCommandAsync();
            if (row > 0) return true;
            return false;
        }

    }
}
